package com.yxsd.web.spring;

import com.pandawork.core.bean.StaticAutoWire;
import com.pandawork.core.exception.SSException;
import com.pandawork.core.web.spring.controller.Base;
import com.yxsd.service.*;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 添加抽象父类，进行封装核心的信息。
 * 
 * @author Lionel
 * @E-mail lionel@pandawork.net
 * @time 2012-8-14
 */
public class AbstractController extends Base {

    protected int pageSize = 20;

    /**
     * 当前用户ID
     */
    private ThreadLocal<Integer> hostId = new ThreadLocal<Integer>();
    private ThreadLocal<String> hostName = new ThreadLocal<String>();
    private ThreadLocal<Integer> uId = new ThreadLocal<Integer>();

    // ${website}
    private ThreadLocal<String> website = new ThreadLocal<String>();

    //TODO:在用户确认之后修改此方法
    public void init(HttpServletRequest request, HttpServletResponse response) {
        super.init(request, response); // 执行父类的初始化

       /* Integer partyId = (Integer) request.getAttribute(WebConstants.WebPartyId);
        hostId.set(partyId);
        String name = (String) request.getAttribute(WebConstants.WebUserName);
        hostName.set(name);
        Integer userId = (Integer) request.getAttribute(WebConstants.WebUId);
        uId.set(userId);*/
        Cookie[] cookies = request.getCookies();
        /*for(Cookie cookie : cookies) {
            if(WebConstants.WebPartyId.equals(cookie.getName())) {
                hostId.set(Integer.valueOf(cookie.getValue()));
            } else if(WebConstants.WebUserName.equals(cookie.getName())) {
                hostName.set(cookie.getName());
            } else if(WebConstants.WebUId.equals(cookie.getName())) {
                uId.set(Integer.valueOf(cookie.getValue()));
            }
        }*/

        website.set((String) request.getAttribute("website"));
    }

    /**
     * 获取用户ID,如果当前用户不登录，默认返回0；
     *
     * @author Lionel
     * @E-mail lionel@pandawork.net
     * @time 2012-8-14
     * @return
     */
    protected Integer getHostId() {
        if (hostId.get() == null) {
            return 0;
        }
        return hostId.get();
    }

    protected  String getHostName() {
        return this.hostName.get();
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    protected Integer getUId() {
        if(uId.get() == null) {
            return 0;
        }
        return uId.get();
    }

    /**
     * 获取当前网址
     * @return
     */
    protected String getWebsite() {
        return this.website.get();
    }

    /**
     * 发送json对象
     * @param json
     * @return
     */
    public JSONObject sendJsonObject(JSON json, int code){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", code);
        if(json != null){
            jsonObject.put("data",json);
        }
        return jsonObject;
    }

    // 发送ajax错误信息
    public JSONObject sendErrMsgAndErrCode(SSException e){
        JSONObject json = new JSONObject();
        json.put("code", 1);
        json.put("errMsg", e.getMessage());
        return json;
    }

    // 发送ajax分页信息
    public JSONObject sendJsonArray(JSONArray jsonArray,int dataCount){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("list",jsonArray);
        jsonObject.put("dataCount",dataCount);
        return jsonObject;
    }

    public JSONObject sendSuccessMsg() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        return jsonObject;
    }

    @StaticAutoWire
    protected static AdminService adminService;

    @StaticAutoWire
    protected static MemberService memberService;

    @StaticAutoWire
    protected static OrderService orderService;

    @StaticAutoWire
    protected static ProductService productService;

    @StaticAutoWire
    protected static SupplierService supplierService;
}
